﻿<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>SmartX Wallet</title>

    <!-- Bootstrap -->
    <link href="bootstrap/bootstrap.min.css" rel="stylesheet">
    <link href="js/mystyle.css" type="text/css" rel="stylesheet">


    <!-- 自定义样式 -->
    <style>
        .nav_box {
            height: 60px;
            position: relative;
            top: calc(50% - 30px);
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .qx_1 {
            display: flex;
            line-height: 70x;
            margin: -15px 5px 0 5px;
            padding: 0 18px 0 18px;
            font-size: 14px;
            position: relative;
        }

        body {
            padding-bottom: 48px;
            background: #fafafa;
        }

        .jumbotron {
            background-size: cover;
            padding-top: 7%;
            padding-bottom: 2%;
            background: #e5e5e5;
        }
    </style>
    
    <style>
        fieldset {
            background-color: #f1f1f1;
            border: none;
            border-radius: 2px;
            margin-bottom: 12px;
            overflow: hidden;
            padding: 0 .625em;
        }

        label {
            cursor: pointer;
            display: inline-block;
            padding: 3px 6px;
            text-align: left;
            width: 180px;
            vertical-align: top;
        }

        input {
            font-size: inherit;
            width: 300px;
        }
    </style>

<style>
.bBFMKx {
    box-sizing: border-box;
    margin: 0px;
    min-width: 0px;
    width: 100%;
    display: flex;
    padding: 0px;
    -webkit-box-align: center;
    align-items: center;
    flex-wrap: wrap;
    -webkit-box-pack: center;
    justify-content: center;
}
.gByVwA {
    box-sizing: border-box;
    margin-top: 4px;
    min-width: 0px;
    width: 100%;
    border-radius: 16px;
    padding: 40px;
    border: 1px solid rgb(245, 245, 245);
    background-color: rgb(228 , 228 , 228 );
}
element.style {
    margin-top: 4px;
}
</style>

<style>
body {
    min-height: 100vh;
    background-position: 50% center;
    background-size: 100%;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-image: url(././MoonSwap_files/bg-main.13b7b0cd.png);
}
.flat-form {
  background: #e74c3c;
  margin: 25px auto;
  width: 460px;
  height: auto;
  position: relative;
  font-family: 'Roboto';
  position: relative;
    max-width: 460px;
    width: 100%;
    background: rgb(255, 255, 255);
    box-shadow: rgba(0, 0, 0, 0.03) 0px 0.64rem 2.56rem 0.85rem, rgba(0, 0, 0, 0.05) 0px 0.48rem 1.49rem 0px, rgba(0, 0, 0, 0.08) 0px 0.32rem 0.85rem -0.43rem;
    border-radius: 30px;
    padding: 1rem;
}

.tabs {
    display: flex;
    flex-flow: row nowrap;
    -webkit-box-align: center;
    align-items: center;
    border-radius: 3rem;
    justify-content: space-evenly;
    padding-left: 0px;   
}

.tabs li {
  display: block;
  float: left;
  margin: 0;
  padding: 0;
}
.tabs a {
    display: contents;
    flex-flow: row nowrap;
    -webkit-box-align: center;
    align-items: center;
    -webkit-box-pack: center;
    justify-content: center;
    height: 3rem;
    border-radius: 3rem;
    outline: none;
    cursor: pointer;
    text-decoration: none;
    color: rgb(136, 141, 155);
    font-size: 20px;
}

.tabs li:last-child a {
  border-right: none;
  width: 174px;
  padding-left: 0;
  padding-right: 0;
  text-align: center;
}

.tabs a.active {
    border-radius: 12px;
    font-weight: 500;
    color: rgb(51, 51, 51);
}
.form-action {
  padding: 0 20px;
  position: relative;
}

.form-action h1 {
  font-size: 42px;
  padding-bottom: 10px;
}
.form-action p {
  font-size: 12px;
  padding-bottom: 10px;
  line-height: 25px;
}
form {
  padding-right: 20px !important;
}

.dark-box {
  background: #5e0400;
  box-shadow: 1px 3px 3px #3d0100 inset;
  height: 40px;
  width: 50px;
}
.form-action .dark-box.bottom {
  position: absolute;
  right: 0;
  bottom: -24px;
}
.tabs + .dark-box.top {
  position: absolute;
  right: 0;
  top: 0px;
}
.show {
  display: block;
}
.hide {
  display: none;
}

.button {
    border: none;
    display: block;
    background: #136899;
    height: 40px;
    width: 50%;
    color: #ffffff;
    text-align: center;
    border-radius: 5px;
    /*box-shadow: 0px 3px 1px #2075aa;*/
  	-webkit-transition: all 0.15s linear;
	  -moz-transition: all 0.15s linear;
	  transition: all 0.15s linear;
}

.button:hover {
  background: #1e75aa;
  /*box-shadow: 0 3px 1px #237bb2;*/
}

.button:active {
  background: #136899;
  /*box-shadow: 0 3px 1px #0f608c;*/
}

::-webkit-input-placeholder {
  color: #e74c3c;
}
:-moz-placeholder {
  /* Firefox 18- */
  color: #e74c3c;
}
::-moz-placeholder {
  /* Firefox 19+ */
  color: #e74c3c;
}
:-ms-input-placeholder {
  color: #e74c3c;
}
 </style>

</head>
<body>
    <script src="bootstrap/jquery.min.js"></script>
    <script src="bootstrap/bootstrap.min.js"></script>
    <!--<script type="text/javascript" src="js/crypto/encrypt/base/basex.js"></script>
    <script type="text/javascript" src="js/crypto/encrypt/base/base58.js"></script>
    <script type="text/javascript" src="js/crypto/encrypt/ed25519/nacl-fast.js"></script>
    <script type="text/javascript" src="js/crypto/utils/encrpt-ed25519.js"></script>-->

    <script type="text/javascript" src="crypto-master/js/encrypt/base/basex.js"></script>
    <script type="text/javascript" src="crypto-master/js/encrypt/base/base58.js"></script>
    <script type="text/javascript" src="crypto-master/js/encrypt/ed25519/nacl-fast.js"></script>
    <script type="text/javascript" src="crypto-master/js/utils/encrypt-ed25519-2.js"></script>

    <script src='js/bignumber.min.js'></script>
    <script type="text/javascript" src="js/crypto-js.js"></script>
    <script src='js/forge-sha256.min.js'></script>
    <script src='js/hashes.js'></script>
    <script src='js/wallet.js'></script>
    <script src='js/helper.js'></script>
    <script src='js/Login.js'></script>

    <div class="container">

        <div class="jumbotron">
            <h1>SmartX-dotnet <small id="statsLabel" style="font-size: 50%"></small></h1>
            <button type="button" class="btn btn-primary" onclick='OnRefresh()'>刷新</button>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <button type="button" class="btn btn-info" onclick='OnCreateMyPair()'>创建质押合约</button>

        </div>

        <div style="width: 60%" class="container">
            <div class="input-group">
                <span class="input-group-addon">合约</span>
                <input type="text" id="searchText" class="form-control">
                <span class="input-group-btn">
                    <button class="btn btn-default" type="button" onclick='liOnSearch("searchText")'>Search!</button>
                </span>
            </div>
        </div>

        <div style="width:100%;height:100%;overflow-x:auto;">
            <table class="table table-hover" id="mypledge">
                <caption id="pledgeLabel">我的质押:</caption>
                <thead>
                    <tr>
                        <th>合约</th>
                        <th>资金池</th>
                        <th>年化率</th>
                        <th>总额度</th>
                        <th>我的占比</th>
                        <th>我的凭证</th>
                        <th>我的资金</th>
                        <th>状态</th>
                    </tr>
                </thead>
            </table>
        </div>

        <div style="width:100%;height:100%;overflow-x:auto;">
            <table class="table table-hover" id="myrules">
                <caption id="RulesLabel">质押合约:</caption>
                <thead>
                    <tr>
                        <th>合约</th>
                        <th>资金池</th>
                        <th>年化率</th>
                        <th>状态</th>
                    </tr>
                </thead>
            </table>
        </div>

        <div class="modal fade" id="ModalPledge" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="ModalPledgeLabel" aria-hidden="true" style="top:10%;">
            <div class="modal-dialog" style="width:460px;">
                <div class="modal-content">
                    
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <center><h4 class="modal-title" id="myModalLabel">质押合约</h4></center>
                        <center><h4 class="modal-title" id="myLiquidityData" hidden></h4></center>
                    </div>

                    <div class="flat-form">
                        <ul class="tabs">
                            <li>
                                <a href="#swap" class="active">添加</a>
                            </li>
                            <li>
                                <a href="#Diversion" class="active">转移</a>
                            </li>
                            <li>
                                <a href="#Liquidity">取回</a>
                            </li>
                        </ul>
                        <div id="swap" class="form-action show">
                            <div style="height: 8px; user-select:none"></div>
                            <div id="myModalAmount" >余额:</div>
                            <div style="height: 10px; user-select:none"></div>
                            <div class="input-group">
                                <div class="input-group-btn">
                                    <button type="button" id='swap_tokenA_Btn' class="btn btn-default dropdown-toggle" style="height: 36px; width: 82px; background-color: rgb(253 214 91);">
                                        <div id='swap_tokenA'>SAT</div><span class="caret"></span>
                                    </button>
                                </div><!-- /btn-group -->
                                <input type="text" class="form-control" id='swap_A' placeholder="0.0" style="height: 36px;"  >
                            </div><!-- /input-group -->
                            <form>
                                <ul>
                                    <div style="height: 10px; user-select:none"></div>
                                    <h5 id='ModalPledgeParam_1'>合约地址:</h5>
                                    <h5 id='ModalPledgeParam_2'>总额度:</h5>
                                    <h5 id='ModalPledgeParam_3'>年化率:</h5>
                                    <h5 id='ModalPledgeParam_4'>我的额度:</h5>
                                    <h5 id='ModalPledgeParam_5'>节点状态:</h5>
                                    <div style="height: 10px; user-select:none"></div>
                                    <center><button type="button" class="btn btn-primary" onclick="onLiquiditySubmit()" style="width: 50%;">添加</button></center>
                                </ul>
                            </form>
                        </div>
                        <!--/#login.form-action-->
                        <div id="Diversion" class="form-action hide">
                                <div class="input-group">
                                    <h5 id='Diversion_Amount'>可用凭证:</h5>
                                    <span class="input-group-btn">
                                        <h5 id='Diversion_TotalSupply'>凭证占比:</h5>
                                    </span>
                                </div>
                                <div style="height: 10px; user-select:none"></div>
                                <input type="text" id="Diversion_Get" class="form-control" placeholder="填入可用凭证" onkeyup='return onchangeLiquidity_Get(event)' onchange='return onchangeLiquidity_Get(event)'>

                                <div style="height: 10px; user-select:none"></div>

                                <div class="btn-group btn-group-justified" role="group" aria-label="...">
                                    <div class="btn-group" role="group">
                                    <button type="button" class="btn btn-default" onclick="onLiquidityPerClick(event)" >25%</button>
                                    </div>
                                    <div class="btn-group" role="group">
                                    <button type="button" class="btn btn-default" onclick="onLiquidityPerClick(event)" >50%</button>
                                    </div>
                                    <div class="btn-group" role="group">
                                    <button type="button" class="btn btn-default" onclick="onLiquidityPerClick(event)" >75%</button>
                                    </div>
                                    <div class="btn-group" role="group">
                                    <button type="button" class="btn btn-default" onclick="onLiquidityPerClick(event)" >100%</button>
                                    </div>
                                </div>

                                <div style="height: 10px; user-select:none"></div>
                                <h5>转移资产:</h5>
                                <div class="input-group">
                                    <div class="input-group-btn" disabled>
                                        <button type="button" class="btn btn-default dropdown-toggle" style="height: 36px; width: 82px; background-color: rgb(253 214 91);">
                                            <div id='Diversion_tokenA'>SAT</div><span class="caret"></span>
                                        </button>
                                    </div>
                                    <input type="text" class="form-control" id='Diversion_A' placeholder="0.0" style="height: 36px;" disabled>
                                </div>
                                <div style="height: 10px; user-select:none"></div>
                                <h5>转移至:</h5>
                                <input type="text" id="Diversion_Address" placeholder="质押合约地址" class="form-control">
                                <div style="height: 10px; user-select:none"></div>
                                <center><h5 style="coloruser-select:none;color:red">质押转移将会扣除手续费:0.01%</h5></center>
                            <form>
                                <ul>
                                    <div style="height: 15px; user-select:none"></div>
                                    <center><button type="button" class="btn btn-primary" onclick="onDiversionSubmit()" style="width: 50%;">转移</button></center>
                                </ul>
                            </form>

                        </div>
                        <div id="Liquidity" class="form-action hide">
                                <div class="input-group">
                                    <h5 id='Liquidity_Amount'>可用凭证:</h5>
                                    <span class="input-group-btn">
                                        <h5 id='Liquidity_TotalSupply'>凭证占比:</h5>
                                    </span>
                                </div>
                                <div style="height: 10px; user-select:none"></div>
                                <input type="text" id="Liquidity_Get" class="form-control" placeholder="填入可用凭证" onkeyup='return onchangeLiquidity_Get(event)' onchange='return onchangeLiquidity_Get(event)'>

                                <div style="height: 10px; user-select:none"></div>

                                <div class="btn-group btn-group-justified" role="group" aria-label="...">
                                    <div class="btn-group" role="group">
                                    <button type="button" class="btn btn-default" onclick="onLiquidityPerClick(event)" >25%</button>
                                    </div>
                                    <div class="btn-group" role="group">
                                    <button type="button" class="btn btn-default" onclick="onLiquidityPerClick(event)" >50%</button>
                                    </div>
                                    <div class="btn-group" role="group">
                                    <button type="button" class="btn btn-default" onclick="onLiquidityPerClick(event)" >75%</button>
                                    </div>
                                    <div class="btn-group" role="group">
                                    <button type="button" class="btn btn-default" onclick="onLiquidityPerClick(event)" >100%</button>
                                    </div>
                                </div>

                                <div style="height: 10px; user-select:none"></div>
                                <h5>取回资产:</h5>
                                <div class="input-group">
                                    <div class="input-group-btn" disabled>
                                        <button type="button" class="btn btn-default dropdown-toggle" style="height: 36px; width: 82px; background-color: rgb(253 214 91);">
                                            <div id='Liquidity_tokenA'>SAT</div><span class="caret"></span>
                                        </button>
                                    </div>
                                    <input type="text" class="form-control" id='Liquidity_A' placeholder="0.0" style="height: 36px;" disabled>
                                </div>
                                <div style="height: 10px; user-select:none"></div>
                            <form>
                                <ul>
                                    <div style="height: 15px; user-select:none"></div>
                                    <center><button type="button" class="btn btn-primary" onclick="onLiquidityRemove()" style="width: 50%;">取回</button></center>
                                </ul>
                            </form>

                            <div style="height: 24px; user-select:none"></div>
                            <div style="width:100%;height:100%;overflow-x:auto;">
                                <table class="table table-hover" id="ModalPledgeTable">
                                    <caption id="caption_mytable111" style="user-select:none;text-align:center;background-color:#ffeeee;">取回状态:</caption>
                                    <thead id="thead_mytable111">
                                        <tr>
                                            <th style="user-select:none">序号</th>
                                            <th style='user-select:none;text-align:center;'>金额</th>
                                            <th style='user-select:none;text-align:right;'>状态</th>
                                        </tr>
                                    </thead>
                                </table>
                            </div>
                            <div style="height: 24px; user-select:none"></div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="ModalCreate" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="ModalCreateLabel" aria-hidden="true" style="top:10%;">
        <div class="modal-dialog" style="width:460px;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <center><h4 class="modal-title" id="myModalLabel">创建质押合约</h4></center>
                </div>
                <form>
                    <ul>
                        <div style="height: 10px; user-select:none"></div>
                        <h5>资金池:</h5>
                        <div class="input-group">
                            <div class="input-group-btn" disabled>
                                <button type="button" class="btn btn-default dropdown-toggle" style="height: 36px; width: 82px; background-color: rgb(253 214 91);">
                                    <div id='ModalCreate_tokenA'>SAT</div><span class="caret"></span>
                                </button>
                            </div>
                            <input type="text" class="form-control" id='ModalCreate_A' placeholder="0.0" style="height: 36px;">
                        </div>
                        <div style="height: 10px; user-select:none"></div>

                        <div style="height: 24px; user-select:none"></div>
                        <center><button type="button" class="btn btn-primary" onclick="OnCreateMyPair2()" style="width: 50%;">创建</button></center>
                        <div style="height: 10px; user-select:none"></div>
                    </ul>
                </form>
                
            </div>
        </div>
    </div>

    <footer class="navbar-fixed-bottom ">
        <center >
            <div style="width:50%;height:100%;overflow-x:auto;">
                <table class="table table-hover" id="statusbar" style='background:#ffffff'>
                </table>
            </div>
        </center>
        <div class="row nav_box navbar-inverse">
            <div class="qx_1"><a href="index.html"><h3 class="text-primary">钱包</h3></a></div>
            <div class="qx_1"><a href="browser.html"><h3 class="text-success">区块</h3></a></div>
            <div class="qx_1"><a href="rules.html"><h3 class="text-danger">节点</h3></a></div>
            <div class="qx_1"><a href="setting.html"><h3 class="text-warning">设置</h3></a></div>
        </div>
    </footer>

    <script>
        (function( $ ) {
            // constants
            var SHOW_CLASS = 'show',
                HIDE_CLASS = 'hide',
                ACTIVE_CLASS = 'active';
            
            $( '.tabs' ).on( 'click', 'li a', function(e){
                e.preventDefault();
                var $tab = $( this ),
                    href = $tab.attr( 'href' );
            
                $( '.active' ).removeClass( ACTIVE_CLASS );
                $tab.addClass( ACTIVE_CLASS );
            
                $( '.show' )
                    .removeClass( SHOW_CLASS )
                    .addClass( HIDE_CLASS )
                    .hide();
                
                $(href)
                    .removeClass( HIDE_CLASS )
                    .addClass( SHOW_CLASS )
                    .hide()
                    .fadeIn( 550 );
            });
        })( jQuery );

        
        function liOnclick(e) {
            //alert(e.id);
        };
        function OnRefresh(e) {
            window.location.href = window.location.href;
        };

        
        function OnCreateMyPair(e) {
            $("#ModalCreate").modal('show');

        }

        function OnCreateMyPair2(e) {
            $.ajax({
                url: Helper.GetServerIP() + "/callFun",
                dataType: "text",
                type: "get",
                data: { consAddress: Helper.PledgeFactory(), data: "getPair(\"" + addressCur + "\")" },
                success: function (data) {
                    OnCreateMyPair_async(data);
                },
                error: function (err) {
                }
            });

        };
        
        function OnCreateMyPair_async(e) {
            if(e==null) {
                alert("合约已存在");
                return;
            }
            let amountADesired = $("#ModalCreate_A").val();

            if(!Helper.CheckAmount(amountADesired,amountCur,"100000")) {
                return;
            }

            let addressOut = Helper.PledgeFactory();
            let data    = 'pairCreated(\"' + Helper.ERCSat() + '\",\"' + Helper.Fix(amountADesired) + '\")';
            let depend  = '';
            let remarks = '';

            console.log( data );

            var hash = Helper.SendTransferSubmit(null, "contract", '', addressOut, data, depend, nonceCur, remarks);
            Helper.Statusbar("statusbar",hash,"","创建质押合约: " + Helper.Fix(amountADesired), 1, nonceCur);
            Helper.MessageBox('创建质押合约已提交');
            $("#ModalCreate").modal('hide');
        }

        function JSONLength(obj) {
            var size = 0, key;
            for (key in obj) {
                if (obj.hasOwnProperty(key)) size++;
            }
            return size;
        };

        if (Login.LoadPassword() == null)
            window.location.href = "index.html";
            
        // 地址栏参数
        function getQueryVariable(variable, search) {
            if (search == null)
                search = window.location.search;
            var query = search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) { return pair[1]; }
            }
            return (false);
        }

        var addressCur   = getQueryVariable("address");
        var tokenAddress = "";
        var amountCur    = 0;
        var nonceCur     = 0;
        var colorindex = 1
        var colorlist = ["list-group-item-success", "", "list-group-item-info", "", "list-group-item-warning", "", "list-group-item-danger", ""];
        var platform = Helper.checkPlatform();

        function OnRefreshAmount() {
            $.ajax({
                url: Helper.GetServerIP() + "/balanceOf",
                dataType: "text",
                type: "get",
                data: { Address: addressCur, token: "" },
                success: function (data) {
                    var jsonObj = JSON.parse(data);
                    var amount = new BigNumber(jsonObj["amount"]).toFormat();
                    var nonce = parseInt(jsonObj["nonce"]);
                    amountCur = amount;
                    nonceCur  = nonce + 1;
                },
                error: function (err) {
                }
            });
            setTimeout(function(){OnRefreshAmount();}, 3000 );
        }
        OnRefreshAmount();

        let curHeight = 1;
        function QueryStats() {
            $.ajax({
                url: Helper.GetServerIP() + "/Stats",
                dataType: "text",
                type: "get",
                data: { style: "1" },
                success: function (data) {

                    var element = document.getElementById("statsLabel");
                    element.innerHTML = data;
                    
                    var height = data.split(" ")[0].replace("H:","");
                    curHeight = parseInt(height);

                },
                error: function (err) {
                }
            });
        }
        QueryStats();        
        
        let jsonRules = null;
        $.ajax({
            url: Helper.GetServerIP() + "/Rules",
            dataType: "text",
            type: "get",
            data: { get: "all" },
            success: function (data) {

                var jsonObj = JSON.parse(data);
                jsonRules = jsonObj;
                var index = 0;
                for (var key in jsonObj) {

                    var color = colorlist[colorindex % colorlist.length]; colorindex++;
                    if(jsonObj[key]["Contract"]!=null)
                    {
                        index++;

                        var annualizedYield = new BigNumber( Helper.Div(Helper.GetRewardRule(),jsonObj[key]["Amount"]) ).toFormat(0);

                        var item = Helper.TableInsert("myrules", jsonObj[key]["Contract"], color,
                            Helper.Simplify(jsonObj[key]["Contract"], platform),
                            jsonObj[key]["Amount"],
                            Helper.Fix(annualizedYield)+"%",
                            "良好");
                        item.innerHTML = item.innerHTML.replace(/liOnclick/g, "liOnPledge");                            
                    }
                }

                var element = document.getElementById("RulesLabel");
                element.innerHTML = "质押合约: " + index;

                onLiquidityInit();
            },
            error: function (err) {
                //alert("提交失败");
            }
        });
        
        function onLiquidityInit() {
            colorindex = 1;

            $.ajax({
                url: Helper.GetServerIP() + "/GetLiquidity",
                dataType: "text",
                type: "get",
                data: { Address: addressCur,Factory: Helper.PledgeFactory(),Pair:"PledgePair"},
                success: function (data) {
                    onLiquidityInit_async(data);
                },
                error: function (err) {
                    alert("没有找到可用节点");
                }
            });
        }
        async function onLiquidityInit_async(data) {
            if (data != "{\"ret\":\"failed\"}") {

                colorindex = 1;
                var jsonObj = JSON.parse(data);
                for (var key in jsonObj) {
                    var array    = JSON.parse(jsonObj[key]);
                    if(array[7]==false)
                        continue;
                    var address = array[0];
                    var symbol1 = array[1];

                    let liquidity = new BigNumber(array[5]);
                    let totalSupply = new BigNumber(array[6]);

                    let per = (liquidity.multipliedBy(100).dividedBy(totalSupply).toFormat());
                    if(per=="NaN")
                        per = "0";

                    var state = "离线";
                    for (var key2 in jsonRules) {
                        if(jsonRules[key2]["Contract"]==address)
                        {
                            state = "良好";
                            break;
                        }
                    }

                    var annualizedYield = new BigNumber( Helper.Div(Helper.GetRewardRule(),array[3]) ).toFormat(0);

                    var profit = Helper.Div( Helper.Mul(array[3], liquidity), totalSupply) ;

                    var color = colorlist[(colorindex - 1) % colorlist.length]; colorindex++;
                    var item  = Helper.TableInsert("mypledge", address, color, address, array[3],Helper.Fix(annualizedYield)+"%",totalSupply,Helper.Fix(per)+"%",liquidity,Helper.Fix(profit),state);
                    item.innerHTML = item.innerHTML.replace(/liOnclick/g, "liOnPledge");
                }
            }
        }

        function liOnPledge(e) 
        {
            ShowPledgePair(e.id);
        }

        function liOnSearch(e) 
        {
            var text = $('#' + e).val();

            if (text != "") {
                ShowPledgePair(text);
            }
        }

        function ShowPledgePair(text) 
        {
            var calldata = "liquidityOf(\"" + addressCur + "\",\"" + Helper.PledgeFactory() + "\")";

            $.ajax({
                url: Helper.GetServerIP() + "/callFun",
                dataType: "text",
                type: "get",
                data: { consAddress: text, data: calldata },
                success: function (data) {
                    var jsonObj = JSON.parse(data);
                    console.log(jsonObj);

                    let liquidity = new BigNumber(jsonObj[5]);
                    let totalSupply = new BigNumber(jsonObj[6]);
                    let per = (liquidity.multipliedBy(100).dividedBy(totalSupply).toFormat());
                    if(per=="NaN")
                        per = "0";
                    per = Helper.Fix(per);

                    var annualizedYield = new BigNumber( Helper.Div(Helper.GetRewardRule(),jsonObj[3]) ).toFormat(0);

                    var state = "离线";
                    for (var key2 in jsonRules) {
                        if(jsonRules[key2]["Contract"]==text)
                        {
                            state = "良好";
                            break;
                        }
                    }

                    $("#ModalPledge").modal('show');
                    $("#myModalAmount").html("余额: " + amountCur);
                    $("#ModalPledgeParam_1").html("地址: " + text);
                    $("#ModalPledgeParam_2").html("总凭证: "  + jsonObj[3]);
                    $("#ModalPledgeParam_3").html("年化率: "  + Helper.Fix(annualizedYield) + "%");
                    $("#ModalPledgeParam_4").html("我的凭证: " + Helper.Fix(liquidity));
                    $("#ModalPledgeParam_5").html("节点状态: " + state);

                    $("#myLiquidityData").html( data );
                    $('#swap_A').val('');
                    
                    $("#Liquidity_Amount").html("可用凭证: " + Helper.Fix(liquidity));
                    $("#Liquidity_TotalSupply").html( "凭证占比:"+ per + '%' );
                    $("#Liquidity_Get").val('');
                    $("#Liquidity_A").val('');

                    $("#Diversion_Amount").html("可用凭证: " + Helper.Fix(liquidity));
                    $("#Diversion_TotalSupply").html( "凭证占比:"+ per + '%' );
                    $("#Diversion_Get").val('');
                    $("#Diversion_A").val('');
                    $('#Diversion_Address').val('');
                    
                    onModalPledgeTable(jsonObj);
                },
                error: function (err) {
                }
            });
        }

        TableInsert3 = function () 
        {
            var innerHTML = "<tbody><tr class='mycolor' id='myid' onclick='liOnclick(this)'>";
            innerHTML = innerHTML.replace(/myid/g, arguments[1]);
            innerHTML = innerHTML.replace(/mycolor/g, arguments[2]);

            innerHTML += "<td style='vertical-align:middle;'>" + arguments[3] + "</td>";
            innerHTML += "<td style='text-align:center;vertical-align:middle;'>" + arguments[4] + "</td>";
            innerHTML += "<td style='text-align:right;vertical-align:middle;'>" + arguments[5] + "</td.amount>";
            innerHTML += '</tr ></tbody>';

            var item_new = document.createElement("tbody");

            item_new.innerHTML = innerHTML;
            document.getElementById(arguments[0]).appendChild(item_new);
        }
        
        function onModalPledgeTable(jsonLiquidity)
        {
            $.ajax({
                url: Helper.GetServerIP() + "/callFun",
                dataType: "text",
                type: "get",
                data: { consAddress: jsonLiquidity[0], data: "allowance(\"" + addressCur + "\")" },
                success: function (data) {
                    var jsonObj = JSON.parse(JSON.parse(data));
                    //console.log(jsonObj);

                    // 删除之前的数据
                    var mytableEle = document.getElementById("ModalPledgeTable");
                    for (var i = mytableEle.children.length - 1; i >= 0; i--) {
                        if (mytableEle.children[i].id.indexOf("_mytable111") == -1)
                            mytableEle.children[i].remove();
                    }

                    colorindex = 1;
                    for (var key in jsonObj) {
                        var color = colorlist[(colorindex - 1) % colorlist.length]; colorindex++;

                        var state = "";
                        if(parseInt(jsonObj[key].LockHeight)<curHeight) {
                            state = "<button type=\"button\" class=\"btn btn-danger\" onclick='liOnPledgeRetrieved("+ jsonObj[key].LockHeight +")'>取回</button>";
                        }
                        else {
                            var height = parseInt(jsonObj[key].LockHeight) - curHeight;
                            if(new BigNumber(height / 240).toFormat(0)!="0")
                                state = new BigNumber(height / 240).toFormat(0) + "小时后可取回"
                            else
                            if(new BigNumber(height / 240).multipliedBy(60).toFormat(0)!="0")
                                state = new BigNumber(height / 240).multipliedBy(60).toFormat(0) + "分钟后可取回"
                            else
                                state = "小于1分钟后可取回"
                        }
                        TableInsert3("ModalPledgeTable", jsonObj[key].LockHeight, color, jsonObj[key].LockHeight, jsonObj[key].Amount,state);
                    }
                },
                error: function (err) {
                }
            });
        }

        function onLiquidityPerClick(e) 
        {
            let per = e.currentTarget.innerHTML.replace("%","");
            
            let array = JSON.parse($("#myLiquidityData").html());
            let reserveA = new BigNumber(array[3]);
            let reserveB = new BigNumber(array[4]);
            let liquidity = new BigNumber(array[5]);
            let totalSupply = new BigNumber(array[6]);

            let bigper = liquidity.multipliedBy(new BigNumber(per)).dividedBy(100);
            $("#Liquidity_Get").val(bigper.toFormat());
            $("#Diversion_Get").val(bigper.toFormat());
            
            reserveA = reserveA.multipliedBy(bigper).dividedBy(totalSupply).toFormat();
            if(reserveA=="NaN") {
                reserveA = "0";
            }

            $("#Liquidity_A").val(Helper.Fix(reserveA));
            $("#Diversion_A").val(Helper.Fix(reserveA));
        }

        function onchangeLiquidity_Get(e)
        {
            let text = $("#"+e.currentTarget.id).val();

            let array = JSON.parse($("#myLiquidityData").html());
            let reserveA = new BigNumber(array[3]);
            let reserveB = new BigNumber(array[4]);
            let liquidity = new BigNumber(array[5]);
            let totalSupply = new BigNumber(array[6]);

            let bigper = new BigNumber(text);

            reserveA = reserveA.multipliedBy(bigper).dividedBy(totalSupply).toFormat();
            if(reserveA=="NaN") {
                reserveA = "0";
            }

            $("#Liquidity_Get").val(text);
            $("#Diversion_Get").val(text);

            $("#Liquidity_A").val(Helper.Fix(reserveA));
            $("#Diversion_A").val(Helper.Fix(reserveA));
        }

        function onLiquidityRemove() 
        {
            let array = JSON.parse($("#myLiquidityData").html());
            let liquidity = $("#Liquidity_Get").val();

            let addressOut = array[0];
            let data    = 'removeLiquidity(\"' + Helper.Fix(liquidity) + '\")';
            let depend  = '';
            let remarks = '';

            console.log( addressOut + ' ' + data );

            if(!Helper.CheckAmount(liquidity,array[5])) {
                return;
            }

            var hash = Helper.SendTransferSubmit(null, "contract", '', addressOut, data, depend, nonceCur, remarks);
            $("#ModalPledge").modal('hide');
            Helper.Statusbar("statusbar",hash,"","合约: " + addressOut +" 取回质押: " + Helper.Fix(liquidity),1,nonceCur);
            Helper.MessageBox('取回质押已提交');
        }

        function onDiversionSubmit()
        {
            let array = JSON.parse($("#myLiquidityData").html());
            let liquidity = $("#Liquidity_Get").val();
            let diversion = $("#Diversion_Address").val();

            let addressOut = array[0];
            let data    = 'diversionLiquidity(\"' + Helper.Fix(liquidity) + '\",\"' + diversion +'\")';
            let depend  = '';
            let remarks = '';

            console.log( addressOut + ' ' + data );

            if(!Helper.CheckAmount(liquidity,array[5])) {
                return;
            }

            var hash =  Helper.SendTransferSubmit(null, "contract", '', addressOut, data, depend, nonceCur, remarks);
            $("#ModalPledge").modal('hide');
            Helper.Statusbar("statusbar",hash,"","合约: " + addressOut +" 转移质押: " + Helper.Fix(liquidity) ,1,nonceCur);
            Helper.MessageBox('转移质押已提交');
        }

        function onLiquiditySubmit()
        {
            let array = JSON.parse($("#myLiquidityData").html());
            let amountADesired = $('#swap_A').val();
            let swap_B_Min = amountADesired;

            if(!Helper.CheckAmount(amountADesired,amountCur)) {
                return;
            }

            let addressOut = array[0];
            let data    = 'addLiquidity(\"' + Helper.Fix(amountADesired) + '\",\"' + Helper.Fix(swap_B_Min) +'\")';
            let depend  = '';
            let remarks = '';
            
            console.log( addressOut + ' ' + data );
            
            var hash =  Helper.SendTransferSubmit(null, "contract", '', addressOut, data, depend, nonceCur, remarks);
            $("#ModalPledge").modal('hide');
            Helper.Statusbar("statusbar",hash,"","合约: " + addressOut +" 增加质押: " + Helper.Fix(amountADesired),1,nonceCur);
            Helper.MessageBox('增加质押已提交');
        }

        function liOnPledgeRetrieved(LockHeight) 
        {
            let array = JSON.parse($("#myLiquidityData").html());
            let addressOut = array[0];
            let data    = 'retrieved(\"' + LockHeight + '\")';
            let depend  = '';
            let remarks = '';

            console.log( addressOut + ' ' + data );

            var hash =  Helper.SendTransferSubmit(null, "contract", '', addressOut, data, depend, nonceCur, remarks);
            $("#ModalPledge").modal('hide');
            Helper.Statusbar("statusbar",hash,"","合约: " + addressOut +" 取回质押,序号: " + LockHeight, 1, nonceCur);
            Helper.MessageBox('取回质押已提交');

        }


        Helper.Statusbar();
    </script>



</body>
</html>

